$(function() {
	$("#projectType").select2({
		placeholder : "请选择"
	});

	setValidate($("#frm"), {
		rules : {
			mobile : {
				telAndPhone : true
			}
		},
		messages : {}
	});

	$("#cSelect2").select2({
		placeholder : cSize == 0 ? "该物业公司无所属物业管理协会, 请先录入" : "请选择"
	});

	// 所属物业管理协会
	$('#cSelect2').select2({
		placeholder : "请选择"
	});

	// 公司资质
	$('#aptitude').select2({
		placeholder : "请选择"
	});

	// 初始化省市区下拉框
	initProvinceSeletor();
	$("#s_province option[value='" + curProvince + "']").attr("selected", "selected");
	var _provinceIndex = $("#s_province option:selected").attr("index");
	if (_provinceIndex && _provinceCache[_provinceIndex]) {
		initCitySelector(_provinceCache[_provinceIndex].children);
	}

	$("#s_city option[value='" + curCity + "']").attr("selected", "selected");
	var _cityIndex = $("#s_city option:selected").attr("index");
	if (_cityIndex) {
		initAreaSelector(_provinceCache[_provinceIndex].children[_cityIndex].children);
	} else {
		initAreaSelector(null);
	}
	$("#s_area option[value='" + curArea + "']").attr("selected", "selected");
	$("#s_province").change(function() {
		var index = $(this).children("option:selected").attr("index");
		if (index) {
			initCitySelector(_provinceCache[index].children);
		} else {
			initCitySelector(null);
		}

		$("#areaCodes").val($(this).val());
	});
	$("#s_city").change(function() {
		var p_index = $("#s_province").children("option:selected").attr("index");
		var index = $(this).children("option:selected").attr("index"); /* <![CDATA[ */
		if (p_index >= 0 && index >= 0) { /* ]]> */
			initAreaSelector(_provinceCache[p_index].children[index].children);
		} else {
			initAreaSelector(null);
		}

		$("#areaCodes").val($("#s_province").val() + "," + $(this).val());
	});
	$("#s_area").change(function() {
		$("#areaCodes").val($("#s_province").val() + "," + $("#s_city").val() + "," + $(this).val());
	});
	$("#s_province").select2({
		placeholder : "请选择省份"
	});
	$("#s_city").select2({
		placeholder : "请选择城市"
	});
	$("#s_area").select2({
		placeholder : "请选择区县"
	});

	if (licence) {
		//showUploadTip(fileServerUrl + licence, "uploadifyButton3");
		$("#uploadifyButton3").attr('src',fileServerUrl + licence);
	}

	if (logoFileno) {
		//showUploadTip(fileServerUrl + logoFileno, "uploadifyButton2");
		$("#uploadifyButton2").attr('src',fileServerUrl + logoFileno);
	}

	if (certificate) {
		//showUploadTip(fileServerUrl + certificate, "uploadifyButton4");
		$("#uploadifyButton4").attr('src',fileServerUrl + certificate);
	}
	
	// 上传公司log
	$("#uploadifyButton2").html5Upload({
		url:'/IMS/fileUpload.ht',
		fileTypeExts:['.gif','.jpg','.png'],
		onUploadSuccess:function(data,filename){
			if(data){
				if(data.code == '1'){
					_alert(data.message);
				}else{
					if (data.result) {
						$("#tempFileName").val(data.result[0]);
					}
				}
			}
		}
	});
	/*1
	$("#uploadifyButton2").uploadify({
		'swf' : "/IMS/js/plugins/file/uploadify/uploadify.swf",
		'uploader' : "/IMS/fileUpload.ht",
		'buttonText' : '',
		'fileTypeDesc' : '当前允许上传格式：*.jpg | *.gif | *.png',
		'fileTypeExts' : '*.gif; *.jpg; *.png',
		'queueID' : 'fileQueue2',
		'auto' : true,
		'multi' : false,
		'width' : 270,
		'height' : 200,
		'method' : 'post',
		'fileSizeLimit' : '10MB',
		'onUploadSuccess' : function(fileData, jsonData) {
			var obj;
			if (jsonData) {
				obj = eval('(' + jsonData + ')');
				if (obj.code == 1) {
					alert(obj.message);
				} else {
					if (obj.result) {
						$("#uploadifyButton2-button img").remove();
						$("#uploadifyButton2-button").append("<img src='" + obj.result[0] + "'/>");
						$("#tempFileName").val(obj.result[0]);
					}
				}
			}
		}
	});
	*/

	// 上传营业执照图
	$("#uploadifyButton3").html5Upload({
		url:'/IMS/fileUpload.ht',
		fileTypeExts:['.gif','.jpg','.png'],
		onUploadSuccess:function(data,filename){
			if(data){
				if(data.code == '1'){
					_alert(data.message);
				}else{
					if (data.result) {
						$("#tempFileName2").val(data.result[0]);
					}
				}
			}
		}
	});
	/*
	$("#uploadifyButton3").uploadify({
		'swf' : "/IMS/js/plugins/file/uploadify/uploadify.swf",
		'uploader' : "/IMS/fileUpload.ht",
		'buttonText' : '',
		'fileTypeDesc' : '当前允许上传格式：*.jpg | *.gif | *.png',
		'fileTypeExts' : '*.gif; *.jpg; *.png',
		'queueID' : 'fileQueue3',
		'auto' : true,
		'multi' : false,
		'width' : 270,
		'height' : 200,
		'method' : 'post',
		'fileSizeLimit' : '10MB',
		'onUploadSuccess' : function(fileData, jsonData) {
			var obj;
			if (jsonData) {
				obj = eval('(' + jsonData + ')');
				if (obj.code == 1) {
					alert(obj.message);
				} else {
					if (obj.result) {
						$("#uploadifyButton3-button img").remove();
						$("#uploadifyButton3-button").append("<img src='" + obj.result[0] + "'/>");
						$("#tempFileName2").val(obj.result[0]);
					}
				}
			}
		}
	});
	*/
	
	// 上传资质证书
	$("#uploadifyButton4").html5Upload({
		url:'/IMS/fileUpload.ht',
		fileTypeExts:['.gif','.jpg','.png'],
		onUploadSuccess:function(data,filename){
			if(data){
				if(data.code == '1'){
					_alert(data.message);
				}else{
					if (data.result) {
						$("#tempFileName3").val(data.result[0]);
					}
				}
			}
		}
	});
	/*
	$("#uploadifyButton4").uploadify({
		'swf' : "/IMS/js/plugins/file/uploadify/uploadify.swf",
		'uploader' : "/IMS/fileUpload.ht",
		'buttonText' : '',
		'fileTypeDesc' : '当前允许上传格式：*.jpg | *.gif | *.png',
		'fileTypeExts' : '*.gif; *.jpg; *.png',
		'queueID' : 'fileQueue4',
		'auto' : true,
		'multi' : false,
		'width' : 270,
		'height' : 200,
		'method' : 'post',
		'fileSizeLimit' : '10MB',
		'onUploadSuccess' : function(fileData, jsonData) {
			var obj;
			if (jsonData) {
				obj = eval('(' + jsonData + ')');
				if (obj.code == 1) {
					alert(obj.message);
				} else {
					if (obj.result) {
						$("#uploadifyButton4-button img").remove();
						$("#uploadifyButton4-button").append("<img src='" + obj.result[0] + "'/>");
						$("#tempFileName3").val(obj.result[0]);
					}
				}
			}
		}
	});
	*/	
});

// 公司介绍输入字数限制提示
function tishi() {
	var len = $("textarea[name='companyInfo']").val().length;
	if (len > 200) {
		$("textarea[name='companyInfo']").val($("textarea[name='companyInfo']").val().substring(0, 200));
		return;
	}
	$("#tishi").empty();
	$("#tishi").text("最多输入200字，您还可以输入" + ((200 <= len) ? 0 : (200 - len)) + "字");
}

// 荣誉输入字数限制提示
function tishi1() {
	var len = $("textarea[name='honorInfo']").val().length;
	if (len > 200) {
		$("textarea[name='honorInfo']").val($("textarea[name='honorInfo']").val().substring(0, 200));
		return;
	}
	$("#tishi1").empty();
	$("#tishi1").text("最多输入200字，您还可以输入" + ((200 <= len) ? 0 : (200 - len)) + "字");
}

// 打开上级公司树
function openParTree() {
	var url = '/IMS/treehtml.ht?temp=par';
	var title = '选择公司';
	jbox.open(url, title);
}

// 初始化省市区下拉框
function initProvinceSeletor() {
	$("#s_province option").remove();
	var citys;
	$.each(_provinceCache, function(index) {
		var optionDom = "<option value='" + this.code + "' ";
		if (index == 0) {
			optionDom += "selected='" + "seleceted' "
		}
		optionDom += "index=" + index + ">" + this.name + "</option>";
		$("#s_province").append(optionDom);
	});
	initCitySelector(citys);
}

function initCitySelector(citys) {
	$("#s_city option").remove();
	var areas;

	$("#s_city").append("<option value='0' selected='selected'>请选择</option>");
	if (citys && citys.length > 0) {
		$.each(citys, function(index) {
			var optionDom = "<option value='" + this.code + "' ";
			optionDom += "index=" + index + ">" + this.name + "</option>";
			$("#s_city").append(optionDom);
		});
		$("#div_city").show();
		$("#s_city").select2({
			placeholder : "请选择城市"
		});
	} else {
		$("#div_city").hide();
	}
	initAreaSelector(areas);
}

function initAreaSelector(areas) {
	$("#s_area option").remove();
	$("#s_area").append("<option value='0' selected='selected'>请选择</option>");
	if (areas && areas.length > 0) {
		$.each(areas, function(index) {
			var optionDom = "<option value='" + this.code + "'>" + this.name + "</option>";
			$("#s_area").append(optionDom);
		});
		$("#div_area").show();
		$("#s_area").select2({
			placeholder : "请选择区县"
		});
	} else {
		$("#div_area").hide();
	}
}
// 初始化省市区下拉框 END

// 跳转部门机构
function enterDep() {
	var url = "/IMS/depart/showList.ht";
	url = url + "?cid=" + cid + "&companyId=" + pcid;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

// 跳转第三方支付
function upd() {
	var url = "/IMS/thirdPayment/showAlipayInfoByP.ht?pcid=" + pcid;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

// 跳转资产管理
function upAss(id) {
	var url = "/IMS/assetsInformation/assetsList.ht?pcid=" + pcid;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

// 修改提交
function saveUserSubmit() {
	var obj = document.forms['frm'];
	if (chkNull("companyName", "公司不能为空")) {
		return;
	}
	if (chkNull("atnName", "公司简称不能为空")) {
		return;
	}

	if (userType == '3') {
		_alert("小区管理员无法添加公司信息");
		return;
	}
	if (userType == '2') {
		if (chkNull("parCompanyName", "上级公司不能为空")) {
			return;
		}
	}

	var pcid = $("#pcid").val();
//	if false {
	if (pcid != null && pcid != "" && pcid != undefined) {
		var parCompanyNum = $("#parCompanyNum").val();
        // _alert("成功");
//		if (parCompanyNum == pcid) {
//			_alert("上级公司不能为自己");
//			return;
//		}

		var form = $('#frm');
		$.submit(form, {
			dataType : 'json',
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data, textStatus) {
				var code = data.messageCode;
				if (message_success_code == code) {
					var url = '/IMS/estate/showList.ht';
					$.go(url, {
						dataType : 'html',
						error : function() {
							hideProccessBar();
						},
						success : function(data, textStatus) {
							replaceDocument(url, data);
						}
					});
				} else {
					_alert(message_false);
					hideProccessBar();
				}
			}
		});
	} else {
		var companyName = $("#companyName").val();
		$.ajax({
			url : "/IMS/estate/searchowner.ht",
			data : {
				companyName : companyName
			},
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data) {
				if (data.messageCode == message_false_code) {
					_alert(data.message);
					hideProccessBar();
					return;
				} else {
					var form = $('#frm');
					$.submit(form, {
						dataType : 'json',
						error : function() {
							hideProccessBar();
						},
						success : function(data, textStatus) {
							var code = data.messageCode;
							if (message_success_code == code) {
								var url = '/IMS/estate/showList.ht';
								_alert("新增成功，请等待自动刷新", function() {
									$.go(url, {
										dataType : 'html',
										error : function() {
											hideProccessBar();
										},
										success : function(data, textStatus) {
											replaceDocument(url, data);
										}
									});
								});
							} else {
								_alert(message_false);
								hideProccessBar();
							}
						}
					});
				}
			}
		});
	}
}